package com.template.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.template.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * MyBatis Plus自动填充处理器
 * 
 * @author template
 * @since 2024-01-01
 */
@Slf4j
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.debug("开始插入填充...");
        
        // 创建时间
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        // 更新时间
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        // 创建人（这里可以从当前登录用户获取，暂时使用默认值）
        this.strictInsertFill(metaObject, "createBy", String.class, "system");
        // 更新人
        this.strictInsertFill(metaObject, "updateBy", String.class, "system");
        // 删除标记
        this.strictInsertFill(metaObject, "deleted", Integer.class, CommonConstant.NOT_DELETED);
        // 版本号
        this.strictInsertFill(metaObject, "version", Integer.class, 1);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.debug("开始更新填充...");
        
        // 更新时间
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        // 更新人（这里可以从当前登录用户获取，暂时使用默认值）
        this.strictUpdateFill(metaObject, "updateBy", String.class, "system");
    }
}
